//
//  FirstHomeBtn.m
//  CarStore
//
//  Created by 千锋 on 16/1/26.
//  Copyright (c) 2016年 1000phone. All rights reserved.
//

#import "FirstHomeBtn.h"

#define CZImageRidio 0.7

@implementation FirstHomeBtn

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.contentMode = UIViewContentModeCenter;
        PRINT;
    }
    return self;
}

// 取消高亮事件
- (void)setHighlighted:(BOOL)highlighted {};

- (void)setTitle:(NSString *)title image:(UIImage *)image forState:(UIControlState)state
{
    [self setTitle:title forState:state];
    [self setImage:image forState:state];
}

// 修改按钮内部子控件的frame
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat distance = self.bounds.size.height * 0.1;
    CGFloat height = self.bounds.size.height * 0.9;
    // 1.imageView
    CGFloat imageX = 0;
    CGFloat imageY = distance;
    CGFloat imageWH = height * CZImageRidio;
    self.imageView.frame = CGRectMake(imageX, imageY, imageWH, imageWH);
    CGPoint imageCenter = self.imageView.center;
    imageCenter.x = self.bounds.size.width/2;
    self.imageView.center = imageCenter;
    
    
    // 2.title
    CGFloat titleX = 0;
    CGFloat titleY = imageWH + distance ;
    CGFloat titleW = self.bounds.size.width;
    CGFloat titleH = self.bounds.size.height - titleY;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
/** 自定义添加按钮事件*/
- (void)addTarget:(id)target action:(SEL)action
{
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
@end
